package com.tous.tool.utils;


import java.util.ArrayList;
import java.util.List;

/**
 *
 *  接口实现
 * @param <T>
 */
public interface  ITree <T> {

    boolean isRoot(T t);

    boolean isChild(T t, T child);

    void setChilde(T t, List<T> list);

    default List<T> toTree(List<T> list){
        List<T> treeList = new ArrayList<>();
        for (T t : list) {
            if (isRoot(t)) {
                treeList.add(t);
                List<T> child = getChild(t, list);
                setChilde(t, child);
            }
        }

        return treeList;
    }
    default List<T> getChild(T t, List<T> list){
        List<T> child = new ArrayList<>();
        for (T t1 : list) {
            if (isChild(t, t1)) {
                child.add(t1);
            }
        }
        if (child.size() > 0) {
            for (T t1 : child) {
                List<T> childNext = getChild(t1, list);
                setChilde(t1, childNext);
            }
        }
        return child;
    }
}
